package com.ipharma.gui;

import java.awt.Color;

import com.ipharma.application.services.transaccion.TransactionExtract;
import com.ipharma.assignments.Assignment;
import com.ipharma.dto.Product;

/**
 * A Swing-based panel class.
 * <P>
 * 
 * @author JorgE.H
 */
public class GUITransactionExtract extends GUITransaction {

   /**
    * Attributes
    */
   Assignment currentAssign;

   /**
    * Panel
    */

   /**
    * Constructs a new instance.
    */
   public GUITransactionExtract(GUIOperation pContainer) {

      super(pContainer);
   }

   /**
    * Adds an item to the transaction. Shows the transaction on screen.
    * 
    * @param pProd
    *           the product
    */
   public void addProduct(Product pProd, int pQty) {

      Product l_prod = (Product) pProd.clone();
      l_prod.setQuantity(pQty);
      if (selectedProducts == null)
         selectedProducts = new TransactionExtract(theSess);
      selectedProducts.addItem(l_prod);

      jDetailArea.setText(jDetailArea.getText() + pProd.getDescription()
            + " - " + pQty);

   }

   /**
    * Sets the assignment for the products in transaction
    * 
    * @param pAssign
    *           the Assignment
    */
   public void setAssignment(Assignment pAssign) {

      if (pAssign == null) {

         selectedProducts = new TransactionExtract(theSess);
         jDetailArea.setText("");
         jLabelTitle.setText(Texts.TSN_TIT_PANEL);
         jLabelTitle.setOpaque(false);
         jLabelTitle.setForeground(Color.black);
         currentAssign = pAssign;
      } else if (pAssign != currentAssign) {
         selectedProducts = new TransactionExtract(theSess);
         selectedProducts.setAssignment(pAssign);
         jLabelTitle.setText(pAssign.toString().substring(0,
               java.lang.Math.min(pAssign.toString().length(), 24)));
         jLabelTitle.setOpaque(true);
         jLabelTitle.setBackground(Color.black);
         jLabelTitle.setForeground(Color.white);
         jDetailArea.setText("");
         currentAssign = pAssign;
      }
   }
}
